package team.star.werespos.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.star.werespos.exception.GlobalException;
import team.star.werespos.mapper.TableMapper;
import team.star.werespos.model.entity.Table;
import team.star.werespos.model.enums.CodeMsgEnum;
import team.star.werespos.model.enums.TableStatusEnum;
import team.star.werespos.service.TableService;

import java.util.List;
import java.util.Objects;

/**
 * @author hdonghong
 * @since 2019/05/18
 */
@Slf4j
@Service
public class TableServiceImpl implements TableService {

    @Autowired
    private TableMapper tableMapper;

    @Override
    public Table getOne(Integer tableId) {
        Table table = tableMapper.selectById(tableId);
        if (table == null) {
            log.error("【查看餐桌】tableId = {}, CodeMsgEnum.TABLE_NOT_EXIST = {}", tableId, CodeMsgEnum.TABLE_NOT_EXIST);
            throw GlobalException.create(CodeMsgEnum.TABLE_NOT_EXIST);
        }
        return table;
    }

    @Override
    public Table getByTableNum(Integer tableNum) {
        Table table = tableMapper.selectOne(
                new QueryWrapper<Table>().eq("table_num", tableNum)
        );
        if (table == null) {
            log.error("【根据桌号查看餐桌】tableNum = {}, CodeMsgEnum.TABLE_NOT_EXIST = {}", tableNum, CodeMsgEnum.TABLE_NOT_EXIST);
            throw GlobalException.create(CodeMsgEnum.TABLE_NOT_EXIST);
        }
        return table;
    }

    @Override
    public void insertOne(Table table) {
        Integer count = tableMapper.selectCount(
                new QueryWrapper<Table>().eq("table_num", table.getTableNum())
        );
        if (!Objects.equals(0, count)) {
            log.error("【新增餐桌】重复桌号，table = [{}]", table);
            throw GlobalException.create(CodeMsgEnum.TABLE_NUM_REPEAT);
        }
        table.setTableStatus(TableStatusEnum.AVAILABLE.getStatus());
        Integer result = tableMapper.insert(table);
        if (Objects.equals(0, result)) {
            log.error("【新增餐桌】失败！result = [{}], table = [{}]", result, table);
            throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
        }
    }

    @Override
    public List<Table> listTables() {
        return tableMapper.selectList(
                new QueryWrapper<Table>().orderByAsc("table_num")
        );
    }

    @Override
    public void updateById(Table table) {
        Integer result = tableMapper.updateById(table);
        if (Objects.equals(0, result)) {
            log.error("【修改餐桌】失败！result = [{}], table = [{}]", result, table);
            throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
        }
    }

    @Override
    public void deleteById(Table table) {
        Integer result = tableMapper.deleteById(table);
        if (Objects.equals(0, result)) {
            log.error("【删除餐桌】失败！result = [{}], table = [{}]", result, table);
            throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
        }
    }

    @Override
    public void releaseTable(Integer tableNum) {
        Table table = getByTableNum(tableNum);
        table  = new Table().setTableId(table.getTableId()).setTableStatus(TableStatusEnum.AVAILABLE.getStatus());
        Integer result = tableMapper.updateById(table);
        if (Objects.equals(0, result)) {
            log.error("【清理餐桌】失败！result = [{}], table = [{}]", result, table);
            throw GlobalException.create(CodeMsgEnum.DATABASE_EXCEPTION);
        }
    }
}
